require.config({
    baseUrl: 'js/common',
    paths: {
        "deptData": "../data/deptData",
        "zhuguanData": "../data/zhuguanData",
        "pagin": "./pagin",
        "monthData": '../data/monthData',
        "buzhuData": '../data/buzhuData',
        "moneyData": "../data/moneyData",
        "validate": "./validate"
    }
});

require(['zhuguanData', 'pagin', 'monthData', 'buzhuData', "moneyData", "validate"], function(ZG, pagin, M, BZ, Money, VD) {
    // var baseUrl = defaultStr();
    /*----------
     *ajax数据请求
     *部门页面管理————补助页面
     *部门页面管理————补发页面
     *默认打开该页的时候就已经知道了是那个部门
     *并且只显示该部门的信息
     */

    var buzhu_table = new Vue({
        el: '#main-data',
        data: {
            items: [],
            /*是将显示在已添加列表中的数据*/
            new_item: [],
            tijiao_items: [],
            query: {
                "rows": 5,
                "page": 1,
                "deptId": localStorage.deptId,
            },
            query_tijiao: {
                "currentPage": 0,
                "pageSize": 5,
                "statusCode": 0,
                "helpDate": '',
                "deptId": localStorage.deptId,
                "type": 0
            },
            check_id: [],
            pages: [],
            curPage: 1,
            pageNum: 0, //总页数
            tijiao_pages: [],
            tijiao_curPage: 1,
            temp: '',
            shenhe_items: [],
            dates: '',
            maxJe: 0,
            check_idAll: false,
            tijiaoNav: false,
            nav: false
        },
        methods: {
            /*初始化页面*/
            init: function() {
                var _self = this;
                _self.queryData();
                _self.queryMonth();
                _self.queryTijiaoData();
                Money.getMoeny().done(function(data) {
                    _self.maxJe = data.body.je;

                })
            },
            queryMonth: function() {
                var _self = this;
                _self.dates = [];
                M.getMonth().done(function(result) {
                    $.each(result.body, function(i, value) {
                        if (parseInt(value.helpMonth) >= 10) {
                            var temp = value.helpYear + '' + value.helpMonth
                        } else {
                            var temp = value.helpYear + '0' + value.helpMonth

                        }
                        _self.dates.push(temp);
                    });

                })
            },
            /*用户提交查询信息，查询助管信息*/
            queryData: function() {
                var _self = this;
                /*提交数据到服务器 query是要查询的数据对象*/
                _self.new_item = [];
                _self.query.stuId = _self.query_stuId;
                _self.query.name = _self.query_name
                ZG.queryZG(_self.query).done(function(result) {

                    $.each(result.result, function(i, val) {
                        val.status = '未审核';
                        _self.new_item.push(val)
                    });
                    /*分页*/
                    if (result.total > _self.query.rows) {
                        /*如果总数大于10就分页*/
                        /*_self.totalPage 总页数*/
                        _self.nav = true;
                        var totalPage = Math.ceil(result.total / _self.query.rows);
                        _self.pageNum = totalPage;
                        var result = pagin.paginGetli(totalPage, _self.curPage);
                        _self.pages = result.pages;
                    } else {
                        _self.pages = [];
                    }
                });
            },
            /*根据row和page请求数据*/
            getDataPage: function(cur_page) {
                var _self = this;
                console.log(cur_page)
                _self.query.page = cur_page;
                _self.curPage = cur_page;
                _self.queryData();
            },
            /*处理助管列表分页查询*/
            submitPage: function(p, b) {
                var _self = this;
                if (b) {
                    return false;
                }
                if (p === ">>") {
                    _self.curPage++;
                    _self.getDataPage(_self.curPage);
                } else if (p === "<<") {
                    _self.curPage--;
                    _self.getDataPage(_self.curPage);
                } else if (p === "...") {
                    if (_self.curPage > _self.pageNum) {
                        _self.getDataPage(_self.pageNum);
                    } else {
                        _self.getDataPage(_self.curPage + 5);
                    }

                } else {
                    _self.curPage = p;
                    _self.getDataPage(_self.curPage);
                }
            },
            /*添加补助到未提交审核,限制年月和金额*/
            join_validate: function() {
                var _self = this;

                _self.add_item.helpDate = _self.temp.helpDate;
                _self.add_item.money = parseInt(_self.temp.money);
                _self.add_item.remark = _self.temp.remark;
                var new_addItem = {
                    "helpDate": _self.add_item.helpDate,
                    "deptId": _self.add_item.deptId,
                    "stuId": _self.add_item.stuId,
                    "szlbdm": 10,
                    "shztm": 1,
                    "remark": _self.add_item.remark,
                    "money": _self.add_item.money
                };
                if (VD.maxNum(parseInt(_self.add_item.money), parseInt(_self.maxJe), "金额") && VD.isNull(_self.add_item.helpDatem, "月份")) {
                    // alert("金额合法")
                    BZ.addBZ(new_addItem).done(function(result) {
                        if (result.status === 1) {
                            _self.queryTijiaoData();
                            $('#myModal').modal('hide');
                        } else {
                            alert(result.message);
                        }
                    })
                };
            },
            /*添加补助到未提交审核,不限制年月和金额*/
            join() {
                var _self = this;
                _self.add_item.type = 0;
                BZ.addBZW(_self.add_item).done(function(result) {
                    if (result.status === 1) {
                        _self.queryTijiaoData();
                        $('#myModal').modal('hide');
                    } else {
                        alert(result.message);
                    }
                })
            },
            /*新写的join方法*/
            join_new: function(item, event) {
                var _self = this;
                var el = event.currentTarget;
                var new_addItem = {
                    "helpDate": _self.choose_helpDate,
                    "deptId": item.deptId,
                    "stuId": item.stuId,
                    "szlbdm": 10, //这两个参数不知道干啥的
                    "shztm": 1,
                    "remark": item.remark,
                    "money": item.money
                };
                if (VD.legalNum(new_addItem.money) && VD.maxNum(parseInt(new_addItem.money), parseInt(_self.maxJe), "金额") && VD.isNull(new_addItem.helpDate, "月份")) {
                    BZ.addBZ(new_addItem).done(function(result) {
                        if (result.status === 1) {
                            _self.queryTijiaoData();
                            $(el)[0].disabled = true;
                        } else {
                            alert(result.message);
                        }
                    })
                };
                console.log(item)
            },
            setItem: function(item) {
                var _self = this;
                _self.add_item = item;
            },
            /*提交数据*/
            submitData: function() {
                var _self = this;
                BZ.submitBZ(_self.check_id).done(function(result) {
                    alert(result.message);

                    _self.queryTijiaoData();
                    _self.check_idAll = false;
                })
            },
            checkIdAll: function() {
                var _self = this;
                _self.check_id = []
                if (!_self.check_idAll) {
                    $.each(_self.tijiao_items, function(i, val) {
                        _self.check_id.push(val.id)
                    });
                } else {
                    _self.check_id = []
                }
            },
            /*导出表格*/
            export: function() {
                window.open("./download-form.html")
            },
            /*查询已提交信息*/
            queryTijiaoData: function() {
                var _self = this;
                _self.tijiao_items = [];
                _self.tijiao_pages = [];
                BZ.queryBuZhu(_self.query_tijiao).done(function(result) {
                    $.each(result.body.result, function(i, val) {
                        _self.tijiao_items.push(val)
                    });
                    if (result.body.total > _self.query_tijiao.pageSize) {
                        /*如果总数大于10就分页*/
                        /*_self.totalPage 总页数*/
                        _self.tijiaoNav = true;
                        var totalPage = Math.ceil(result.body.total / _self.query_tijiao.pageSize);
                        var result = pagin.paginGetli(totalPage, _self.tijiao_curPage);
                        _self.tijiao_pages = result.pages;
                    }
                })
            },
            /*根据row和page请求数据*/
            getTijiaoPage: function(cur_page) {
                var _self = this;
                _self.query_tijiao.currentPage = cur_page - 1;
                _self.tijiao_curPage = cur_page;
                _self.queryTijiaoData();
            },
            /*处理助管列表分页查询*/
            submitTijiaoPage: function(p, b) {
                var _self = this;
                if (b) {
                    return false;
                }
                if (p === ">>") {
                    _self.tijiao_curPage++;
                    _self.getTijiaoPage(_self.tijiao_curPage);
                } else if (p === "<<") {
                    _self.tijiao_curPage--;
                    _self.getTijiaoPage(_self.tijiao_curPage);
                } else {
                    _self.tijiao_curPage = p;
                    _self.getTijiaoPage(_self.tijiao_curPage);
                }
            },
        },
        watch: {
            query_stuId: function(val) {
                this.queryData('all');
            },
            query_name: function(val) {
                this.queryData('all');
            },
            check_id: function(val) {
                if (val.length < this.tijiao_items.length) {
                    this.check_idAll = false;
                } else {
                    this.check_idAll = true;

                }
            }
        }
    });
    buzhu_table.init();
});